package announcement.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import to.AnnounceListTO;
import to.AnnouncementTO;
import announcement.model.AnnounceService;

@Controller
public class AnnounceController {
	private AnnounceService service;
	
	public AnnounceController(AnnounceService service) {		
		this.service = service;
	}
	
	//글등록 처리 컨트롤러1
	@RequestMapping("/writeContent.do")
	public ModelAndView writeContent(AnnouncementTO ato){
		ModelAndView mv = null;
		try {
			service.writeContent(ato);
			mv = new ModelAndView("redirect:getContent.do?no="+ato.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message",e.getMessage());
		}	
		return mv;
	}
	@RequestMapping("/list.do")
	public ModelAndView list(@RequestParam(required=false, defaultValue="1")String page) throws Exception{
		System.out.println(page);
		
		ModelAndView mv = null;
		//2. Business Logic 호출 ->model		
		try {
			if(page.equals("")){
				page = "1";
			}
			int pageNo = Integer.parseInt(page);
			AnnounceListTO lto = service.getAnnounceListByPage(pageNo);
			mv = new ModelAndView("board/list.tiles","lto",lto);		
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message", "글목록 조회도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	//글등록 처리 컨트롤러2
	@RequestMapping("/getContent.do")
	public ModelAndView getContent(int no){
		ModelAndView mv = null;
		System.out.println(no);
		
		try {			
			AnnouncementTO ato= service.getContentByNO(no);
			mv = new ModelAndView("board/board_view.tiles","ato",ato);
		} catch (SQLException e) {			
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message",e.getMessage());
		}			
		return mv;
	}
	//글등록 처리 컨트롤러3
	@RequestMapping("/borderView.do")
	public ModelAndView borderView(int no){
		ModelAndView mv = null;	
		try {
			AnnouncementTO ato = service.getContentByNO(no);	
			mv = new ModelAndView("board/board_content_view.tiles","ato",ato);
		} catch (SQLException e) {			
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message",e.getMessage());
		}
		return mv;
		
	}
	
	@RequestMapping("/modifyForm.do")
	public ModelAndView modifyForm(int no){
		ModelAndView mv = null;
		try{
			//2. B.L 처리
			AnnouncementTO ato = service.getContentByNoForForm(no);
			mv = new ModelAndView("board/modify_form.tiles","ato", ato);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 글 수정 처리 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyContent.do")
	public ModelAndView modifyContent(AnnouncementTO ato) {
		ModelAndView mv = null;
		try {			
			//2. B.L 요청
			service.modifyContent(ato);
			mv = new ModelAndView("board/board_content_view.tiles", "ato", ato);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("board/error.tiles","error_message", "수정처리중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/deleteContent.do")	
	public ModelAndView deleteContent(int no, String page){
		ModelAndView mv = null;
		try {
			//2. BL
			service.deleteContentByNO(no);
			//mv = new ModelAndView("/list2.do");
			mv = list(page);
			//mv = list(request, response);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles","error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}	

		//공지사항2
	@RequestMapping("/listShow.do")
	public ModelAndView getMemberListShow(int no){
		ModelAndView mv =null;
		try {
			AnnouncementTO ato = service.getContentByNO(no);
			mv = new ModelAndView("board/list_show.tiles","ato",ato);
		} catch (SQLException e) {		
			e.printStackTrace();	
			mv = new ModelAndView("board/error.tiles","error_message", "오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	//타일로 쓰기폼 보내기
	@RequestMapping("/writeForm.do")
	public String sendContent(){
		return "board/write_form.tiles";
	}
	
	
}
